#!/usr/bin/env python3
#
# ddsketch_conversion.py
#
# This source file is part of the FoundationDB open source project
#
# Copyright 2013-2024 Apple Inc. and the FoundationDB project authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import argparse
import ddsketch_calc as dd


parser = argparse.ArgumentParser(description="Converts values to DDSketch buckets")
parser.add_argument(
    "-e",
    "--error_guarantee",
    help="Error guarantee (default is 0.005)",
    required=False,
    type=float,
)
parser.add_argument("-v", "--value", help="Value", required=False, type=int)
parser.add_argument("-b", "--bucket", help="Bucket index", required=False, type=int)
args = parser.parse_args()

error = 0.005

if args.error_guarantee is not None:
    error = args.error_guarantee

sketch = dd.DDSketch(error)

if args.value is not None:
    print("Bucket index for ", args.value)
    print(sketch.getIndex(args.value))

if args.bucket is not None:
    print("Value for bucket ", args.bucket)
    print(sketch.getValue(args.bucket))
